//
//  XLTabBar.swift
//  SinaProjectTest
//
//  Created by zhanghaifeng on 16/10/22.
//  Copyright © 2016年 BBSWaitting. All rights reserved.
//

import UIKit

class XLTabBar: UITabBar {

    // 01定义一个闭包 撰写按钮使用
    var closure:(() -> ())?
    
    
    // 手写创建tabBar 使用init(frame)
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    // 支持xib sb 创建的类
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: btn的点击事件
    @objc private func btnClick(){
        // 03 闭包的使用  执行闭包
        closure?()
    }
    
    // MARK: 设置视图
    func setupUI(){
        // 添加子控件
        addSubview(composeButton)
    }
    // 在layout 中给自定义的btn布局
    override func layoutSubviews() {
        super.layoutSubviews()
        // 设置UITabBarBtn的宽度
        let composeBtnW = XLSCREENW * 0.2
        var index: CGFloat = 0
        for value in subviews{
            // 判断是否是我们想要的UITabBarButton
            if value.isKind(of: NSClassFromString("UITabBarButton")!) {
                // 设置x w
                value.frame.size.width = composeBtnW
                value.frame.origin.x = index * composeBtnW
                index += 1
                if index == 2 {
                    index += 1
                }
            }
            
        }
        // 设置撰写按钮的frame
        composeButton.center.x = XLSCREENW * 0.5
        composeButton.center.y = frame.size.height * 0.5
    }
    
    // MARK: 懒加载控件
    lazy var composeButton: UIButton = {
        // 创建按钮
        let btn = UIButton(setImageNamed: "tabbar_compose_icon_add", setBackgroundImage: "tabbar_compose_button", target: self, action: #selector(btnClick))
        return btn
    }()
}
